import { getCurrentInstance } from "vue";
import { deleteFileApi } from "../api/file_api";
import { useUserStore } from "../stores/user"; // 导入 useUserStore 函数，用于获取用户信息，如 token 等，用于请求接口时传递给后端验证用户身份。
import { useCode } from "./codeHook";

export function useFile() {
  const userStore = useUserStore();
  const { tackleCode } = useCode();
  const proxy: any = getCurrentInstance()?.proxy;
  const deleteFile = async (data: { id: number; url: string }) => {
    let request = {
      token: userStore.token,
      fileId: data.id,
      fileUrl: data.url,
    };
    await deleteFileApi(request).then((res) => {
      if (tackleCode(res.data.code)) {
        proxy.$message({ type: "success", message: "删除成功" });
      }
    });
  };

  return {
    deleteFile,
  };
}
